<?php

require_once("functions.php");

//Adds a single keyword to the database
function addKeyword($newKeyword)
{
	$database = getDB();
	
	$newKeyword = arrayEscape($newKeyword, $database);
	
	$statement = "INSERT INTO Keyword SET value = '$newKeyword[value]'";

	$results = $database->query($statement);

	$error = $database->error;

	$database->close();
	
	return $error == '' ? true : array('SQL Error' => $error);
}

function blank_keyword()
{
	return array('id'=>null, 'value'=>null, 'keyword_types'=>null);
}

//Deletes a single keyword from the database
function delKeyword($idKeyword)
{
	$database = getDB();
	
	$statement = "DELETE FROM Keyword WHERE id = '$idKeyword'";

	$results = $database->query($statement);

	$error = $database->error;

	$database->close();
	
	return $error == '' ? true : array('SQL Error' => $error);
}

//Edits a single keyword in the database
function editKeyword($updatearray)
{
	$database = getDB();
	
	$updatearray = arrayEscape($updatearray, $database);

	$statement = "UPDATE Keyword SET value = '$updatearray[value]' WHERE id = '$updatearray[id]'";

	$results = $database->query($statement);

	$error = $database->error;

	$database->close();
	
	return $error == '' ? true : array('SQL Error' => $error);
}

//Retrieves a single keyword from database
function getKeyword($idKeyword)
{
	$database = getDB();

	$statement = "SELECT * FROM Keyword WHERE id = '$idKeyword'";

	$results = $database->query($statement);

	$keyword = $results->fetch_assoc();

	$database->close();
	
	return $keyword;
}

//Retrieves all keywords from database
function getKeywords()
{
	$database = getDB();
	
	$statement = "SELECT * FROM Keyword ORDER BY value ASC";

	$results = $database->query($statement);

	$database->close();
	
	return $results;
}

?>